package com.sky.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.Dao.CategoryDao;
import com.sky.Service.CategoryService;
import com.sky.Service.DishService;
import com.sky.Service.SetmealService;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.exception.BaseException;
import com.sky.result.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl
        extends ServiceImpl<CategoryDao, Category>
        implements CategoryService {

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    //TODO 分页逻辑还需要修改
    @Override
    public PageResult getByPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        IPage<Category> page=new Page<>(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        //创建条件构造器对象
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        //设置查询条件
        queryWrapper.like(categoryPageQueryDTO.getName()!=null,Category::getName,categoryPageQueryDTO.getName());
        queryWrapper.eq(categoryPageQueryDTO.getType()!=null,Category::getType,categoryPageQueryDTO.getType());
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getCreateTime);
        categoryDao.selectPage(page,queryWrapper);
        //新建一个分页结果对象
        PageResult pageResult = new PageResult();
        //设置参数
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page.getRecords());
        return pageResult;
    }

    @Override
    public void addDishCategory(CategoryDTO categoryDTO) {
        //新建一个Category对象
        Category category = new Category();
        //将CategoryDTO对象属性复制到Category对象中
        BeanUtils.copyProperties(categoryDTO,category);
        //设置Status属性为启用
        category.setStatus(StatusConstant.ENABLE);
        categoryDao.insert(category);
    }

    @Override
    public void updateCategoryStatus(Integer status, Long id) {
        //根据id查询Category对象
        Category category = categoryDao.selectById(id);
        //设置Status属性
        category.setStatus(status);
        //更新修改时间
        category.setUpdateTime(LocalDateTime.now());
        //更新修改人
        category.setUpdateUser(BaseContext.getCurrentEmpId());
        //更新Category对象
        categoryDao.updateById(category);
    }

    @Override
    public void modifyCategory(CategoryDTO categoryDTO) {
        Category category = categoryDao.selectById(categoryDTO.getId());
        BeanUtils.copyProperties(categoryDTO,category);
        //更新修改时间
        category.setUpdateTime(LocalDateTime.now());
        //更新修改人
        category.setUpdateUser(BaseContext.getCurrentEmpId());
        //更新Category对象
        categoryDao.updateById(category);
    }

    @Override
    public void deleteById(Long id) {
        //查询当前分类是否关联了菜品，如果已经关联了，则不能删除
        LambdaQueryWrapper<Dish> queryWrapper=new LambdaQueryWrapper<> ();
        //添加查询条件，根据分类id进行查询
        queryWrapper.eq(Dish::getCategoryId,id);
        //查询分类下是否有菜品
        int count = dishService.count(queryWrapper);
        if(count>0){
            //如果已经关联了菜品，则不能删除
            throw new BaseException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        //查询当前分类是否关联了套餐，如果已经关联了，则不能删除
        LambdaQueryWrapper<Setmeal> setmealQueryWrapper=new LambdaQueryWrapper<> ();
        //添加查询条件，根据分类id进行查询
        setmealQueryWrapper.eq(Setmeal::getCategoryId,id);
        //查询分类下是否有套餐
        int count1 = setmealService.count(setmealQueryWrapper);
        if(count1>0){
            //如果已经关联了套餐，则不能删除
            throw new BaseException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //删除分类信息
        categoryDao.deleteById(id);
    }

    @Override
    public List<Category> getCategoryListByType(Integer type) {
        LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper<> ();
        queryWrapper.eq(Category::getType,type);
        List<Category> categories = categoryDao.selectList(queryWrapper);
        return categories;

    }

    @Override
    public List<Category> getCategoryList(Integer[] type) {
        //如果type数组为空，就查询全部数据
        if(type==null){
            LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper<> ();
            //查询起售的分类
            queryWrapper.eq(Category::getStatus, StatusConstant.ENABLE);
            List<Category> categories = categoryDao.selectList(queryWrapper);
            System.out.println("查询到全部数据:"+categories);
            return categories;
        }
        //如果type数组不为空，就查询指定类型的数据
        LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper<> ();
        queryWrapper.eq(Category::getType,type[0]);
        //查询起售的分类
        queryWrapper.eq(Category::getStatus,StatusConstant.ENABLE);
        List<Category> categories = categoryDao.selectList(queryWrapper);
        System.out.println("查询到指定类型数据:"+categories);
        return categories;
    }
}
